package com.koron.operation.classes.shift.bean.po;

import com.alibaba.excel.annotation.ExcelIgnoreUnannotated;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.format.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.koron.bean.annocation.FillField;
import com.koron.operation.classes.shift.converter.DutyConverter;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Date;
import java.util.Optional;

/**
 * 值班记录(ClassesDutyRecord)表实体类
 *
 * @author jzp
 * @since 2022-04-01
 */
@ExcelIgnoreUnannotated
@Data
@ApiModel(value = "值班记录", description = "ClassesDutyRecord")
public class ClassesDutyRecord {

    /**
     * ID
     */
    @FillField
    @ApiModelProperty("ID")
    private String id;

    /**
     * 账号
     */
    @ApiModelProperty("账号")
    private String account;

    /**
     * 姓名
     */
    @ExcelProperty(value = "姓名")
    @ApiModelProperty("姓名")
    private String userName;

    /**
     * 值班单位id
     */
    @ApiModelProperty("值班单位id")
    private String deptId;

    /**
     * 值班单位名称
     */
    @ExcelProperty(value = "值班单位")
    @ApiModelProperty("值班单位名称")
    private String deptName;

    /**
     * 值班地点id
     */
    @ApiModelProperty("值班地点id")
    private String locationId;

    /**
     * 值班地点名称
     */
    @ExcelProperty(value = "值班地点")
    @ApiModelProperty("值班地点名称")
    private String locationName;

    /**
     * 值班班次id
     */
    @ApiModelProperty("值班班次id")
    private String shiftId;

    /**
     * 值班班次名称
     */
    @ExcelProperty(value = "值班班次")
    @ApiModelProperty("值班班次名称")
    private String shiftName;

    /**
     * 值班岗位，数据字典：operation_duty_type
     */
    @ExcelProperty(value = "值班岗位", converter = DutyConverter.class)
    @ApiModelProperty("值班岗位，数据字典：operation_duty_type")
    private String dutyType;

    /**
     * 值班日期
     */
    @ExcelProperty(value = "值班日期")
    @DateTimeFormat("yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
    @ApiModelProperty("值班日期")
    private Date dutyDate;

    /**
     * 上班时间
     */
    @ExcelProperty(value = "上班时间")
    @DateTimeFormat("HH:mm")
    @JsonFormat(pattern = "HH:mm", timezone = "GMT+8")
    @ApiModelProperty("上班时间")
    private Date openingTime;

    /**
     * 下班时间
     */
    @ExcelProperty(value = "下班时间")
    @DateTimeFormat("HH:mm")
    @JsonFormat(pattern = "HH:mm", timezone = "GMT+8")
    @ApiModelProperty("下班时间")
    private Date closingTime;

    /**
     * 上班时长
     */
    @ApiModelProperty("上班时长")
    private Long workDuration;

    @ExcelProperty(value = "上班时长")
    @ApiModelProperty("上班时长字符串 HH时mm分")
    private String workDurationStr;

    /**
     * 创建时间
     */
    @FillField
    @ApiModelProperty("创建时间")
    private Date createTime;

    public String getWorkDurationStr() {
        return Optional.ofNullable(workDuration).map(e -> {
            long h = e / 1000 / 60 / 60;
            long m = e % (1000 * 60 * 60) / 1000 / 60;
            return h + "时" + m + "分";
        }).orElse(null);
    }

    public void build(String account,
                      String userName,
                      String deptId,
                      String deptName,
                      String locationId,
                      String locationName,
                      String shiftId,
                      String shiftName,
                      String dutyType,
                      Date dutyDate,
                      Date openingTime,
                      Date closingTime
                                   ) {
        this.account = account;
        this.userName = userName;
        this.deptId = deptId;
        this.deptName = deptName;
        this.locationId = locationId;
        this.locationName = locationName;
        this.shiftId = shiftId;
        this.shiftName = shiftName;
        this.dutyType = dutyType;
        this.dutyDate = dutyDate;
        this.openingTime = openingTime;
        this.closingTime = closingTime;
        if (closingTime != null && openingTime != null) {
            this.workDuration = closingTime.getTime() - openingTime.getTime();
        }
    }

}
